package com.woniuxy.userassembly.apiimpl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.api.RbacRoleApi;
import com.woniuxy.common.PageResult;
import com.woniuxy.common.Result;
import com.woniuxy.form.RbacRoleForm;
import com.woniuxy.userassembly.model.RbacRole;
import com.woniuxy.userassembly.service.IRbacPermissionRoleService;
import com.woniuxy.userassembly.service.IRbacRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Liyfi
 * @since 2021-05-22
 */
@RestController
public class RbacRoleController implements RbacRoleApi {
    @Autowired
    private IRbacRoleService service;

    @Autowired
    private IRbacPermissionRoleService permissionRoleService;

    /**
     * 新增角色
     * @param form
     * @return
     */
    @Override
    public Result newRole(RbacRoleForm form) {
        RbacRole role = new RbacRole();
        BeanUtils.copyProperties(form,role);
        int i = service.addRole(role);
        if(i  == 1){
            return Result.success("新增角色成功");
        }
        return Result.fail("新增角色失败");
    }

    /**
     * 删除角色
     * @param roleName
     * @param roleId
     * @return
     */
    @Override
    public Result updRole(String roleName,int roleId) {
        int i = service.updRole(roleName,roleId);
        if(i == 1){
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @Transactional
    @Override
    public Result delRole(int roleId) {
       int i = service.delRole(roleId);
        if(i == 1){
            int i1 = service.delRole(roleId);
            return Result.success("删除角色成功");
        }
        return Result.fail("删除角色失败");
    }

    /**
     * 分页查询所有角色
     * @param pageIndex
     * @return
     */
    @Override
    public PageResult findRole(int pageIndex) {
        Page<RbacRole> pageParam = new Page<>(pageIndex,10L);
        IPage<RbacRole> pageResult = service.page(pageParam);
        PageResult<RbacRole> myPageResult = new PageResult<RbacRole>((int) pageResult.getTotal(), (int) pageResult.getCurrent(), (int) pageResult.getSize(), pageResult.getRecords());
        myPageResult.setSuccess(true);
        return myPageResult;
    }

    /**
     * 根据id查询角色
     * @param roleId
     * @return
     */
    @Override
    public Result findRoleById(int roleId) {
        RbacRole role = service.findRoleById(roleId);
        if(role != null){
            return Result.success(role);
        }
        return Result.fail("查无此人");
    }


}

